package usersObjects;

import serializableObjects.*;
import questionsObjects.*;

import java.util.TreeMap;


public class StorageOfUsers {
    private TreeMap<String, User> users;

    public StorageOfUsers() {
        users = new TreeMap<String, User>();
    }

    public void setUserBehavior(UserBehavior currentUserBehavior, UserCertificate currentUserCertificate) {
        getUser(currentUserCertificate).setUserBehavior(currentUserBehavior, currentUserCertificate);
    }

    public void executeUserBehaviors(StorageOfUsers currentStorageOfUser, UserCertificate currentUserCertificate) {
        getUser(currentUserCertificate).executeUserBehaviors(currentStorageOfUser, currentUserCertificate);
    }

    public void setQuestionsForUser(Questions storageOfQuestions, UserCertificate currentUserCertificate) {
        getUser(currentUserCertificate).setQuestionsForUser(storageOfQuestions, currentUserCertificate);
    }

    public Questions getQuestionsForUser(UserCertificate currentUserCertificate) {
        return getUser(currentUserCertificate).getQuestionsForUser(currentUserCertificate);
    }

    public void add(UserCertificate currentUserCertificate) {
        User newUser = new User(currentUserCertificate.getUserLogin(), currentUserCertificate.getUserPassword());
        users.put(newUser.getUserLogin(), newUser);
    }

    public void downloadUserFromFiles(SerializableUser serializableUser) {
        String userLogin = serializableUser.getUserLogin();
        String userPassword = serializableUser.getUserPassword();
        String userScore = serializableUser.getUserScore();

        UserCertificate userCertificate = new UserCertificate(userLogin, userPassword);
        add(userCertificate);

        getUser(userCertificate).setScore(userCertificate, userScore);
    }

    public boolean hasUser(String username) {
        return users.containsKey(username);
    }

    public boolean checkUserPassword(UserCertificate currentUserCertificate) {
        return getUser(currentUserCertificate).checkUserPassword(currentUserCertificate);
    }

    public void setUserScore(UserCertificate currentUserCertificate, String userResult) {
        getUser(currentUserCertificate).setScore(currentUserCertificate, userResult);
    }

    public String getUserScore(UserCertificate currentUserCertificate) {
        return getUser(currentUserCertificate).getScore(currentUserCertificate);
    }

    public void setQuestionHistories(UserCertificate currentUserCertificate, QuestionHistory questionHistory) {
        getUser(currentUserCertificate).setQuestionHistories(currentUserCertificate, questionHistory);
    }

    public QuestionHistories getQuestionHistories(UserCertificate currentUserCertificate) {
        return getUser(currentUserCertificate).getQuestionHistories(currentUserCertificate);
    }

    public SerializableUser getSerializableUser(UserCertificate currentUserCertificate) {
        return getUser(currentUserCertificate).getSerializableUser(currentUserCertificate);
    }

    private User getUser(UserCertificate currentUserCertificate) {
        return users.get(currentUserCertificate.getUserLogin());
    }

}
